from bs4 import BeautifulSoup

html_doc = """
<html>
    <head>
        <title>The Dormouse's story</title>
    </head>
<body>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!--Elsie--></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""


data = BeautifulSoup(html_doc,'lxml')

# find 找到符合条件的第一个
a = data.find('a')
a = data.find(attrs={
    "id":"link2"
})
a = data.find(text="Lacie")

a = data.find(attrs={
    "class":"sister"
})





# find_all  一次性取出 所有符合条件
a = data.find_all('a',limit=1)
a = data.find_all(attrs={
    "class":"story"
})

# a = data.find_all("a")


# select ---css选择器
#  a 标签选择器,  类class; id; 层级选择器; 组选择器

a = data.select("a")
a = data.select(".sister")
a = data.select("#link3")
a = data.select("head title")
a = data.select("title,#link3")


# 取文字
a = data.select("#link3")
text = a[0].get_text()

# 取属性
attrs = a[0].get('href')
print(attrs)